package activemq.queue1;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/*
 * 2019/1/13 15:10
 * by activemq
 */
public class Consumer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory factor=new ActiveMQConnectionFactory(
                ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://localhost:61616");
                //"failover:(tcp://192.168.0.102:61616,tcp://192.168.0.102:61617,tcp://192.168.0.102:61618)?Randomize=false");
        Connection connection = factor.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Session session = connection.createSession(false,Session.CLIENT_ACKNOWLEDGE);

        Destination destination = session.createQueue("queue1");
        MessageConsumer consumer = session.createConsumer(destination);
        System.out.println("Listening...");
        for(int i=0;i<1000;i++)
        {
            TextMessage message = (TextMessage) consumer.receive();
            if(message!=null)
            {
                System.out.println(message.getText());
                //message.acknowledge();
            }
        }
        connection.close();
    }
}
